package com.cslc.wechat.common.service;


import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.wechat.common.dto.menu.Menu;
import com.cslc.wechat.common.dto.message.custommessage.CustomMessage;
import com.cslc.wechat.common.exception.WeChatException;
import com.cslc.wechat.common.mp.MpApi;
import com.cslc.wechat.common.mp.MpInfo;
import com.cslc.wechat.util.WeChatUtil;

public class WechatServiceImpl implements WechatService {
	private static Logger logger = Logger.getLogger(WechatServiceImpl.class.getName());
	@Override
	public JSONObject createMenu(Menu menu) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.CREATE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.CREATE_MENU.getMethod();
		String postData = JSONObject.fromObject(menu).toString();
		logger.debug("自定义菜单请求url：" + requestUrl);
		logger.debug("自定义菜单请求POST内容：" + postData);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject createMenu(String fileName) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.CREATE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.CREATE_MENU.getMethod();
		//从菜单配置文件读取字符串，再进行json格式化
		String postData = JSONObject.fromObject(WeChatUtil.readFile(fileName)).toString();
		logger.debug("自定义菜单请求url：" + requestUrl);
		logger.debug("自定义菜单请求POST内容：" + postData);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject getMenu() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_MENU.getUrl(), access_token);
		String requestMethod = MpApi.GET_MENU.getMethod();
		String postData = null;
		logger.debug("自定义菜单请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject deleteMenu() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.DELETE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.DELETE_MENU.getMethod();
		String postData = null;
		logger.debug("自定义菜单请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject getMaterialCount() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_MATERIAL_COUNT.getUrl(), access_token);
		String requestMethod = MpApi.GET_MATERIAL_COUNT.getMethod();
		String postData = null;
		logger.debug("获取素材总数请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("获取素材总数请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject getUserInfo(String openId) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_USER_INFO.getUrl(), access_token, openId);
		String requestMethod = MpApi.GET_USER_INFO.getMethod();
		String postData = null;
		logger.debug("获取用户基本信息请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("获取用户基本信息请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject getOauth2Acess(String code) {
		JSONObject jsonObject = null;
		String requestUrl = String.format(MpApi.GET_OAUTH2_TOKEN.getUrl(), MpInfo.APP_ID, MpInfo.APP_SECRET, code);
		String requestMethod = MpApi.GET_OAUTH2_TOKEN.getMethod();
		String postData = null;
		logger.debug("通过网页授权获取openid请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("通过网页授权获取openid请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject sendCustomMessage(CustomMessage message) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.SEND_CUSTOM_MESSAGE.getUrl(), access_token);
		String requestMethod = MpApi.SEND_CUSTOM_MESSAGE.getMethod();
		String postData = JSONObject.fromObject(message).toString();
		logger.debug("发送客服消息请求url：" + requestUrl);
		logger.debug("发送客服消息请求POST内容：" + postData);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("发送客服消息请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject getKFList() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_KF_LIST.getUrl(), access_token);
		String requestMethod = MpApi.GET_KF_LIST.getMethod();
		String postData = null;
		logger.debug("获取客服所有账号请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("获取客服所有账号请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject createTempTicket() {
		return null;
		//TODO 创建临时二维码ticket
	}
	@Override
	public JSONObject createPermTicket(String terminalNo) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.CREATE_QRCODE.getUrl(), access_token);
		String requestMethod = MpApi.CREATE_QRCODE.getMethod();
		
		//构造post内容，json格式
		String postData = "{\"action_name\":\"QR_LIMIT_STR_SCENE\",\"action_info\":{\"scene\":{\"scene_str\":\"" +	
							terminalNo + "\"}}}";
		logger.debug("获取ticket请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("获取ticket请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public JSONObject showQRCode(String ticket) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.SHOW_CODE_WITH_TICKET.getUrl(), ticket);
		String requestMethod = MpApi.SHOW_CODE_WITH_TICKET.getMethod();
		String postData = null;
		logger.debug("获取二维码请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("获取二维码请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	@Override
	public void getTempMaterial(String mediaId) {
		JSONObject jsonObject = null;
		String savePath = MpInfo.IMG_PATH;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_TEMP_MATERIAL.getUrl(), access_token, mediaId);
		
		logger.debug("获取临时素材请求url：" + requestUrl);
		
		String filePath = WeChatUtil.getMedia(requestUrl, mediaId, savePath);
		logger.debug("获取临时素材请求返回包： " + filePath);
	}

}
